package com.aiiju.core.cont;

import java.io.InputStream;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.aiiju.core.service.WordService;
import com.aiiju.core.service.tool.IoService;
import com.aiiju.core.service.tool.ParseService;
import com.aiiju.core.service.tool.ReadService;
import com.aiiju.core.util.DeleteFileUtil;
import com.aiiju.core.util.FilePathUtils;
import com.aiiju.core.util.OpenOffice2PDF;
import com.aiiju.core.util.StringTool;
import com.aiiju.core.util.SysUtil;
import com.aiiju.core.util.UuidUtils;
import com.aiiju.core.util.txtParse;
import com.bole.resumeparser.models.TextResumeData;

@Controller
public class UploadController {
	
	@RequestMapping(value="/show.do")
	public String TestControl() {
		
		return "show";
	}
	
	@RequestMapping(value="/parse.do")
	public String TestControl(@RequestParam(required = false)MultipartFile uploadFile ,Model model) throws Exception {
		
		String originalFilename = uploadFile.getOriginalFilename();
		
		boolean blank = StringUtils.isNotBlank(originalFilename);
		if(blank) {
			InputStream input = uploadFile.getInputStream();
			String extensionName = StringTool.getExtensionName(originalFilename);
			
			// 获取上传文件内容
			String fileContent = new WordService().getInputString(input, extensionName);
			System.err.println(fileContent);
			
			// 文本转成对象
			if(StringUtils.isNotBlank(fileContent)) {
				model.addAttribute("fileContext",fileContent);
				
				// 转成对象
				ParseService parseService = new ParseService();
				TextResumeData parseObj = parseService.parseObj(fileContent);
				model.addAttribute("resume",parseObj);
				
				// 转成json
				String parseTxt = parseService.objToJson(parseObj);
				model.addAttribute("jsonContext",parseTxt);
			}
		}
		return "show";
	}

}
